package com.ruoyi.crop.controller;

import com.alibaba.excel.EasyExcel;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.crop.domain.DaoDept;
import com.ruoyi.crop.listener.DaoDataListener;
import com.ruoyi.crop.mapper.DaoDeptMapper;
import com.ruoyi.crop.service.CropService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
@RequestMapping("excel")
public class ExcelController {

    @Autowired
    CropService cropService;
    @Autowired
    DaoDeptMapper daoDeptMapper;

    @RequestMapping("download")
    public void download(HttpServletResponse response) throws IOException {
        // 这里注意 有同学反应使用swagger 会导致各种问题，请直接用浏览器或者用postman
        List<DaoDept> deptList = daoDeptMapper.selectList(null);
        String fileName = DaoDept.class.getSimpleName();
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("utf-8");
        // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
        fileName = URLEncoder.encode(fileName, "UTF-8").replaceAll("\\+", "%20");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
        EasyExcel.write(response.getOutputStream(), DaoDept.class).sheet("部门").doWrite(deptList);
    }

    @RequestMapping("upload")
    public Map<String, Object> upload(MultipartFile file) throws IOException {
        EasyExcel.read(file.getInputStream(), DaoDept.class, new DaoDataListener(daoDeptMapper)).sheet().doRead();
        return null;
    }


    @GetMapping("/baoList")
    public AjaxResult baoList() {
        List<Map<String, Object>> list = cropService.listBao();
        List<String> ydataList = list.stream()
                .map(m -> (String) m.get("ydata"))
                .collect(Collectors.toList());
        List<Long> xdataList = list.stream()
                .map(m -> (Long) m.get("xdata"))
                .collect(Collectors.toList());
        Map<String, Object> mapList = new HashMap<>();
        mapList.put("ydata", ydataList);
        mapList.put("xdata", xdataList);
        return AjaxResult.success(mapList);
    }

//    @GetMapping("baoList")
//    public AjaxResult baoList(){
//        List<Map<String,Object>> list = cropService.listBao();
//        ArrayList<String> nameList = new ArrayList<>();
//        ArrayList<Long> valueList = new ArrayList<>();
//        for (Map<String, Object> stringObjectMap : list) {
//            System.err.println(stringObjectMap);
//            String name = (String) stringObjectMap.get("name");
//            nameList.add(name);
//            Long value = (Long) stringObjectMap.get("value");
//            valueList.add(value);
//        }
//        HashMap<String, Object> mapList = new HashMap<>();
//        mapList.put("name",valueList);
//        mapList.put("value",nameList);
//       return success(mapList);
//    }
}
